/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.entidade.registroimovel;

import br.com.deployment.utilitario.CoordinateConversionEntity;
import br.com.deployment.utilitario.Msg;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 *
 * @author roniere
 */
@Entity
@Table
public class GeoRef implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name = "seq_geo", sequenceName = "seq_geo")
    @GeneratedValue(generator = "seq_geo", strategy = GenerationType.SEQUENCE)
    private int id;
    @Column
    @NotNull
    private double lat;
    @Column
    @NotNull
    private double lng;
    @Column
    @NotNull
    private String convE;
    @Column
    @NotNull
    private String convN;
    @Column
    @NotNull
    private String convZona;

    public GeoRef() {
    }

    public GeoRef(double lat, double lng) {
        this.lat = lat;
        this.lng = lng;
    }

    public GeoRef(double lat, double lng, String convE, String convN, String convZona) {
        this.lat = lat;
        this.lng = lng;
        this.convE = convE;
        this.convN = convN;
        this.convZona = convZona;
    }

    public int getId() {
        return id;
    }

    public String getConvE() {
        return convE;
    }

    public void setConvE(String convE) {
        this.convE = convE;
        convert();
    }

    public String getConvN() {
        return convN;
    }

    public void setConvN(String convN) {
        this.convN = convN;
        convert();
    }

    public String getConvZona() {
        return convZona;
    }

    public void setConvZona(String convZona) {
        this.convZona = convZona;
        convert();
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public void convert() {
        String lat = convN;
        String lon = convE;
        String zona = convZona;

        try {
            lat = lat.replace(" ", "");
            lon = lon.replace(" ", "");
            if (zona == null) {
                zona = "M";
            }

            CoordinateConversionEntity cc = new CoordinateConversionEntity();

            String UTM = lon.toString().substring(0, 2) + " " + zona + " " + lon.toString().substring(2) + " " + lat.toString();
            System.err.println("CONVERTENDO COORDENADA: " + UTM);
            
            setLat(cc.utm2LatLon(UTM)[0]);
            setLng(cc.utm2LatLon(UTM)[1]);
                    

        } catch (Exception e) {
            Msg.messagemError("Valores incorretos");
        }

    }

    @Override
        public String toString() {
        return "GeoRef{" + "id=" + id + ", lat=" + lat + ", lng=" + lng + '}';
    }
    
    
    
    
}
